import { createRouter, createWebHashHistory } from 'vue-router'
import { firstMenu } from '@/utils/map-menus'
const router = createRouter({
  history: createWebHashHistory(),
  routes: [
    {
      path: '/',
      redirect: '/main'
    },
    {
      path: '/main',
      name: 'main',
      component: () => import('../views/main/main.vue')
    },
    {
      path: '/login',
      component: () => import('../views/login/Login.vue')
    },
    {
      // 地址错误情况下，重定向到NotFound.vue页面
      path: '/:pathMathch(.*)',
      component: () => import('../views/not-found/NotFound.vue')
    }
  ]
})
//路由导航守卫
router.beforeEach((to) => {
  if (to.path == '/main') {
    const token = localStorage.getItem('LOGIN_TOKEN')
    // 如果没有token，则重定向到登录页面
    if (!token) {
      return '/login'
    }
    //如果是登录页面，且有token，则重定向到首页第一个菜单
    if (to.path === '/main' && token && firstMenu) {
      return firstMenu.url
    }
  }
})
export default router
